package com.sumanth.trianglecheck;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

import java.lang.Math;

public class TCMain extends Activity {

	private EditText ETAB,ETBC,ETCA;
	private Button B1C,B2E;
	private TextView RES;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcmain);
        
        addClickListener();
    }

    public void addClickListener(){
    	B1C=(Button) findViewById(R.id.button1);
    	B2E=(Button) findViewById(R.id.button3);
    	RES=(TextView) findViewById(R.id.textView4);
    	B1C.setOnClickListener(new View.OnClickListener() {
    	public void onClick(View V){
    		ETAB=(EditText) findViewById(R.id.editText1);
	    	ETBC=(EditText) findViewById(R.id.editText2);
	    	ETCA=(EditText) findViewById(R.id.editText3);	
	    	String s1=ETAB.getText().toString();
	    	String s2=ETBC.getText().toString();
	    	String s3=ETCA.getText().toString();
	    	
	    	if (s1.equals("") || s2.equals("") || s3.equals("")) {
	    		RES.setText("Enter the non-empty and non-zero positive real numbers");
	    		
	    	}
	    	else {
	    		double sd1=Double.valueOf(s1).doubleValue();
		    	double sd2=Double.valueOf(s2).doubleValue();
		    	double sd3=Double.valueOf(s3).doubleValue();
		    	if (sd1<=0 || sd2<=0 || sd3<=0) {
		    		RES.setText("Enter non-zero positive real numbers");
		    	}
		    	else {
		    		if (sd1>=(sd2+sd3) || sd1<=Math.abs(sd2-sd3)) {
		    			RES.setText("Entered values fail to form Triangle");
		    		}
		    		else {
		    			double p=(sd1+sd2+sd3)/2.0;
		    			double area=Math.sqrt(p*(p-sd1)*(p-sd2)*(p-sd3));
		    			String str1=Double.toString(p*2);
		    			String str2=Double.toString(area);
		    			RES.setText("Perimeter:: "+ str1 +"\narea:: "+str2 );
		    		}
		    			
		    	}
	    	}
    		
    	}
    		
    	});
    	B2E.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
    }
    
    
}
